package conf

import (
	"github.com/spf13/viper"
	"honeyweb/log"
)

type MysqlConf struct {
	Username     string `json:"username" toml:"username"`
	Password     string `json:"password" toml:"password"`
	Path         string `json:"path" toml:"path"`
	Dbname       string `json:"dbname" toml:"dbname"`
	Config       string `json:"config" toml:"config"`
	MaxIdleConns int    `json:"maxIdleConns" toml:"maxIdleConns"`
	MaxOpenConns int    `json:"maxOpenConns" toml:"maxOpenConns"`
	LogMode      bool   `json:"logMode" toml:"logMode"`
}

func ReadFromFile(file string) (*MysqlConf, error) {
	v := viper.New()
	v.SetConfigFile(file)
	if err := v.ReadInConfig(); err != nil {
		log.S().Debugf("read mysql config file: %s error: %v", file, err)
		return  nil, err
	}

	var conf MysqlConf
	if err := v.Unmarshal(&conf); err != nil {
		log.S().Debug("Unmarshal mysql config file: %s error: %v", file, err)
		return nil, err
	}
	log.S().Debugf("mysql conf: %+v", conf)
	return  &conf, nil
}
